package com.joshua.spring.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.joshua.spring.entity.Provider;
import com.joshua.spring.entity.dto.ProviderQueryCondition;
import com.joshua.spring.mapper.ProviderMapper;
import com.joshua.spring.service.ProviderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.List;

import static com.joshua.spring.constants.FormatterConstants.FORMATTER3S;
import static com.joshua.spring.constants.SymbolConstants.EMPTY_STR;
import static com.joshua.spring.constants.SymbolConstants.PERCENT_SIGN;

/**
 * projectName feign-test
 * package com.joshua.spring.service.impl
 * classname  ProviderInfoServiceImpl
 * <p>
 * description
 * </p>
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/10/20 15:38
 */
@Slf4j
@Service
public class ProviderInfoServiceImpl implements ProviderInfoService {
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public PageInfo<Provider> query(ProviderQueryCondition condition) {
        PageHelper.startPage(condition.getPage(), condition.getSize());
        String keyWords = EMPTY_STR;
        if (!StringUtils.isEmpty(condition.getInfo())) {
            keyWords = condition.getInfo();
        }
        List<Provider> result = providerMapper.selectByExample(Example.builder(Provider.class)
                .andWhere(Sqls.custom().andLike("info", String.format(FORMATTER3S,
                        PERCENT_SIGN, keyWords, PERCENT_SIGN))).build());
        PageInfo<Provider> providerPageInfo = new PageInfo<>(result);
        return providerPageInfo;
    }

    @Override
    public Integer deleteProviderInfos(List<Integer> ids) {
        int result = providerMapper.deleteByExample(Example.builder(Provider.class)
                .andWhere(Sqls.custom().andIn("providerId", ids)).build());
        return result;
    }

    @Override
    public Integer insertProviderInfo(Provider provider) {
        return providerMapper.insertSelective(provider);
    }

    @Override
    public Integer updateProviderInfo(Provider provider) {
        return providerMapper.updateByPrimaryKeySelective(provider);
    }
}
